/**
 * ...
 * @author HTML5
 */

function Renderer2D() 
{
	this.camera = new Camera();
	this.layers = new Array();
}

Renderer2D.prototype.numDraws = 0;
Renderer2D.prototype.camera = null;
Renderer2D.prototype.layers = null;

Renderer2D.prototype.render = function()
{
	
	var i = 0;
	var j = 0;
	var max = this.layers.length;
	var max2 = 0;
	var layer = null;
	var update = this.camera.update();
	var children = null;
	var field = this.camera.visibleRect;
	var canvas = null;
	var context = null;
	var sprite = null;
	var camera = this.camera;
	var inc = 0;
	var transformData = this.camera.transformData;
	
	
	var camBounds = camera.bounds;
	
		
		
	for( i = 0; i < max; i++ )
	{
		
		layer = this.layers[i];
		
	
		if( ( layer.lazy && !update ) || layer.canvas == null || layer.context == null )
		{
			continue;
		}
		
		
		children = layer.displayList.getVisibleChildrenIn( camBounds.x , camBounds.y, camBounds.width, camBounds.height );
		
		if( !layer.sorted && layer.sortFunc != null )
		{
			children = layer.sortFunc.apply( this, [children].concat( layer.sortArgs ) );
		}
		
		canvas = layer.canvas;
		context = layer.context;
		
		
		context.clearRect( 0, 0, canvas.width, canvas.height );
		context.save();
	
		max2 = children.length;
		
		for( j = 0; j < max2; j++ )
		{
			sprite = children[j];
			sprite.render( context );
		}
		
		inc += max2;
		
		
		context.restore();
	}
	
	this.numDraws = inc;
	
	
	
}